//
//  KitchenerInfoViewController.m
//  BestKitchener
//
//  Created by yang on 14-3-11.
//  Copyright (c) 2014年 Administrator. All rights reserved.
//

#import "KitchenerInfoViewController.h"
#import "ASIHTTPRequest.h"
#import "BIDConstants.h"
#import "BIDCookBook.h"
#import "BIDCookBookCell.h"
#import "CookbookInfoViewController.h"

@interface KitchenerInfoViewController ()

@end

@implementation KitchenerInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.cookBookList = [NSMutableArray array];
        NSArray *tempArray = [self getDataFromWebWithString:@"index.php/CookBook/showKitchenerCookBook?kitchenerid=1"];
        for (NSDictionary *dic in tempArray) {
            BIDCookBook *cookBook = [[BIDCookBook alloc] init];
            cookBook.cookbookName = [dic objectForKey:@"cookbookname"];
            cookBook.matchDate = [dic objectForKey:@"matchdate"];
            //解析照片
            NSMutableString *tempStr = [NSMutableString stringWithString:kHostURL];
            [tempStr appendString:[dic objectForKey:@"cookbookiamge"]];
            NSString *imagePath = tempStr;
            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:imagePath]];
            [request startSynchronous];
            cookBook.cookbookImage = [[UIImage alloc] initWithData:[request responseData]];
            [self.cookBookList addObject:cookBook];
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //实例化厨师类
    self.kitchener = [[BIDKitchener alloc] init];
    
    //设置表视图的datasource和delegate
    self.matchWorksTableView.dataSource = self;
    self.matchWorksTableView.delegate = self;
    //获取网络接口数据
    NSArray *tempArray = [self getDataFromWebWithString:@"index.php/Kitchener/show?kitchenerid=2"];
    NSDictionary *tempDic = [tempArray objectAtIndex:0];
    self.kitchener.kitchenerId = [[tempDic objectForKey:@"kitchenerid"] intValue];
    self.kitchener.kitchenerName = [tempDic objectForKey:@"kitchenername"];
    self.kitchener.kitchenerPopularity = [[tempDic objectForKey:@"kitchenerpopularity"] intValue];
    self.kitchener.kitchenerJoin = [[tempDic objectForKey:@"kitchenerjoin"] intValue];
    self.kitchener.eateryId = [[tempDic objectForKey:@"eateryid"] intValue];
    self.kitchener.kitchenerColor = [[tempDic objectForKey:@"kitchenercolor"] floatValue];
    self.kitchener.kitchenerScent = [[tempDic objectForKey:@"kitchenerscent"] floatValue];
    self.kitchener.kitchenerTaste = [[tempDic objectForKey:@"kitchenertaste"] floatValue];
    //获取厨师头像
    NSMutableString *tempStr = [NSMutableString stringWithString:kHostURL];
    [tempStr appendString:[tempDic objectForKey:@"kitchenerimage"]];
    NSString *imagePath = tempStr;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:imagePath]];
    [request startSynchronous];
    self.kitchener.kitchenerImage = [[UIImage alloc] initWithData:[request responseData]];
    //厨师所属餐厅名
    self.eateryAddressLabel.text = [tempDic objectForKey:@"eateryname"];
    self.kitchenerNameLabel.text = self.kitchener.kitchenerName;
    self.kitchenerColorLabel.text = [NSString stringWithFormat:@"%.1f",self.kitchener.kitchenerColor];
    self.kitchenerScentLabel.text = [NSString stringWithFormat:@"%.1f",self.kitchener.kitchenerScent];
    self.kitchenerTasteLabel.text = [NSString stringWithFormat:@"%.1f",self.kitchener.kitchenerTaste];
    self.kitchenerPopularityLabel.text = [NSString stringWithFormat:@"%d",self.kitchener.kitchenerPopularity];
    self.kitchenerImageView.image = self.kitchener.kitchenerImage;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
#pragma mark - 封装方法
- (NSArray *)getDataFromWebWithString:(NSString *)str
{
    //测试获取网络信息信息
    NSMutableString *tempStr = [NSMutableString stringWithString:kHostURL];
    [tempStr appendString:str];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:tempStr]];
    [request startSynchronous];
    
    //接受网络接口数据
    NSData *tempData = [request responseData];
    return [NSJSONSerialization JSONObjectWithData:tempData options:NSJSONReadingMutableLeaves error:nil];
}
#pragma mark - tableview协议方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.cookBookList count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"default";
    static BOOL registerNib = NO;
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//    }
//    
//    cell.textLabel.text = @"宫爆鸡丁";
    //注册自定义单元格
    if (!registerNib) {
        UINib *nib = [UINib nibWithNibName:@"BIDCookBookCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    //创建单元格
    BIDCookBookCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[BIDCookBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    BIDCookBook *tempCookbook = [self.cookBookList objectAtIndex:indexPath.row];
    cell.cookBookImageView.image = tempCookbook.cookbookImage;
    cell.cookBookNameLabel.text = tempCookbook.cookbookName;
    cell.matchDateLabel.text = tempCookbook.matchDate;
    
    return cell;
}
//选中某个菜谱查看详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CookbookInfoViewController *cookbookVic =[[CookbookInfoViewController alloc] initWithNibName:@"CookbookInfoViewController" bundle:nil];
    [self.navigationController pushViewController:cookbookVic animated:YES];
}
//设置组名和高度
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"参赛作品";
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
//设置单元格高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}
@end
